package com.gitee.wsl.text.regex.sample


/**
 * Regex of exact phone number. Update at 2021.05.13.
 * - China mobile: 134,135,136,137,138,139,147,148,150,151,152,157,158,159,172,178,182,183,184,187,188,195,198
 * - China unicom: 130,131,132,145,146,155,156,166,175,176,185,186,196
 * - China telecom: 133,149,153,173,174,177,180,181,189,191,193,199
 * - China nrta: 190,192,197
 * - China mobile virtual: 165,1703,1705,1706
 * - China unicom virtual: 167,1704,1707,1708,1709,171
 * - China telecom virtual: 162,1700,1701,1702
 */
val Regex.Companion.PHONE_EXACT: Regex
    get() = "^1(3\\d|4[5-9]|5[0-35-9]|6[2567]|7[0-8]|8\\d|9[0-35-9])\\d{8}$".toRegex()


/**
 * An international phone number regex, that does not allow delimiters.
 * - Only numbers are allowed
 * - the "+" sign at the start is required
 * The first group in this regex will be the country code.
 * @see InternationalDelimitedPhoneNumber
 */
val Regex.Companion.InternationalNumericPhoneNumber: Regex by lazy {
    """\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}${'$'}""".toRegex()
}

/**
 * An international phone number regex with delimiters allowed.
 * - Any delimiters including spaces are allowed (\W char in regex). Strip them from your final number.
 * - Otherwise equal to [InternationalNumericPhoneNumber].
 * @See InternationalNumericPhoneNumber
 */
val Regex.Companion.InternationalDelimitedPhoneNumber: Regex by lazy {
    """^\+((?:9[679]|8[035789]|6[789]|5[90]|42|3[578]|2[1-689])|9[0-58]|8[1246]|6[0-6]|5[1-8]|4[013-9]|3[0-469]|2[70]|7|1)(?:\W*\d){0,13}\d$""".toRegex()
}

/**
 * 移动电话
 * eg: 中国大陆： +86  180 4953 1399，2位区域码标示+11位数字
 * 中国大陆 +86 Mainland China
 */
val Regex.Companion.MOBILE: Regex
    get() = "(?:0|86|\\+86)?1[3-9]\\d{9}".toRegex()

/**
 * 中国香港移动电话
 * eg: 中国香港： +852 5100 4810， 三位区域码+10位数字, 中国香港手机号码8位数
 */
val Regex.Companion.MOBILE_HK: Regex
    get() = "(?:0|852|\\+852)?\\d{8}".toRegex()

/**
 * 中国台湾移动电话
 * eg: 中国台湾： +886 09 60 000000， 三位区域码+号码以数字09开头 + 8位数字, 中国台湾手机号码10位数
 * 中国台湾 +886 Taiwan 国际域名缩写：TW
 */
val Regex.Companion.MOBILE_TW: Regex
    get() = "(?:0|886|\\+886)?(?:|-)09\\d{8}".toRegex()

/**
 * 中国澳门移动电话
 * eg: 中国澳门： +853 68 00000， 三位区域码 +号码以数字6开头 + 7位数字, 中国澳门手机号码8位数
 * 中国澳门 +853 Macao 国际域名缩写：MO
 */
val Regex.Companion.MOBILE_MO: Regex
    get() = "(?:0|853|\\+853)?(?:|-)6\\d{7}".toRegex()

/**
 * 座机号码<br></br>
 * pr#387@Gitee
 */
val Regex.Companion.TEL: Regex
    get() = "(010|02\\d|0[3-9]\\d{2})-?(\\d{6,8})".toRegex()

/**
 * 座机号码+400+800电话
 */
val Regex.Companion.TEL_400_800: Regex
    get() = "0\\d{2,3}[\\- ]?[0-9]\\d{6,7}|[48]00[\\- ]?[0-9]\\d{2}[\\- ]?\\d{4}".toRegex()

fun String.isPhone(): Boolean =
    Regex.PHONE_EXACT.matches(this)
